from fastapi import APIRouter, Query
from apps.aps import scheduler

router = APIRouter()


def task_a(x):
    # 待执行的任务
    print(f"执行task-a: {x}")


@router.get('/demo')
async def demo_a():
    return {"ms": "demoa"}


@router.post('/task')
async def add_task_a():
    # 添加任务, 每个任务都有唯一的id
    scheduler.add_job(
        task_a, args=('hello', ), trigger='interval', id='task1', seconds=5
    )
    return {"msg": "任务创建成功！"}


@router.get('/pause')
async def pause_job_a(
        task_id: str = Query(..., title='任务id')
):
    """暂停任务"""
    scheduler.pause_job(task_id)    # task_id 参数校验，先看有没有
    return {"msg": "任务暂停！"}


@router.get('/resume')
async def resume_job_a(
        task_id: str = Query(..., title='任务id')
):
    """暂停重启"""
    scheduler.resume_job(task_id)
    return {"msg": "任务重启！"}